package com.yjy;

import com.alibaba.fastjson.JSONObject;
import com.elimen.common.model.trade.v2.request.ElimenTradeRequest;
import com.elimen.common.model.trade.v2.response.ElimenTradeResponse;
import com.elimen.sdk.tools.RandomStringTools;
import com.elimen.sdk.trade.v2.ElimenTradeClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

public class MainTest {
    public static void main(String[] args) throws Exception {
        String channelKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDT6uW7jn+n8hcru1hnCNfDcMq48CBVna8iDt1BRNHN/HJrbedIE+qa0qHuDOBqjhao5WCKQN9wJwcRlYrl1PfAkshf4qRnSs93w29JwX2vzlfS2fEpfE6FLNetBv84bPIsDkzt7Sae29ee6nHNN1s0manBNP6rnbplyQg3fzIU4fDLN201CDouQvNFJNEqiAxHPVdMCIuZDRCg7XiqBUb68vSIbaE1hVgYq7azizitCWuZVRPaoU2usL3aA+EuMDahWiPcb5lD1RMPsFnI8IJr8CZQKVabfFJKJ32X2ErTrf8eK3Qz+NdUdQDM31UBpedn0mZSMe3Dyu4pPZBf+JZtAgMBAAECggEAae5pmC/LMDCb1Ih50EdjU5b2PywJfcKs4OF9SDsWhVlD+N+fVTBffbT8rPpS0/Di8tp2P6XeCzn3FpsiK3Ehx2mYMBYjCF8lBkDzSHMoWNFUpVm8a3hv57JavWtZ+OK8y7j6S5TYUWCjkBSxflvFg62sZaAQDclye57aNKOobTb6dQ+07rjpm4FOs75/8OeU+zSS6/g48i1ILkBf6t9+eJl6UgnwBrROUHhWXkhSiTw5i9CAFbHqiDx6a5r/aEhO/JwtpK/DV0rFizw5fIuU5wOhuq7SmPQATWBKx7B7nUxjVm47WiK9v5FU3AQLKx7MvDlh0mSUj/ViLcr1KEvxIQKBgQD9FGQHh29OOTwB7feXrH0eUFiHWB9rk+A8EE5VlgoxG109YuMSN1BtIuLc6ZK5hNjDERLVWYrgxr5tDPV0R2/Q5m4kRTSNmMRoSOVjwI2t0+BW+aCSgD/lPELvAd25OQx3cLS2blK4PsZU+ge0m/O9mhbLO/MrZ/tx1Ey0p3pPvwKBgQDWXOlzEZN69SEej+ZdsX8ZdAmF/jN61g1t4yMu/jrnLHqfnQmCzCkgL48ogaaYZqcKXkuHST7u+8WIsGHMFiPtaZCVXty1t5U5Y7E6+yfeA3V0NDWEV7MB9HHHGYxEGjl/51c4PJFeBP94BOjzHImnj4t7/MBcjF6hAOFU2Y4k0wKBgB5u+kxxe/ATL+UngbOqmW0wykSUU0rzgG6Yo2LlZRpcqQy9vIKvRiupm1Bngi5vDEYA4pvnZaz8GWAxw76U7JxZaXtRGy2/5jaINZWWTTNPHFY5NJcJdyWQ5pepSMmCU751PjYF9EqVufH0E0Mj0d7GDeGotuhMW89ncUX0MeAvAoGBAKqcno2gIpKNgYOyonZV7EQU7/N+o/iTX3LjgkBHxhfyddKOCWDaH61nTPi/YBoSMcb10AVoDbfmGY02XVAzGvs9tv671symxhU8PHYmKc/xTs1jNGkV5/qZVOlTcylM+hDLzsaKpmR/c9lRhxrfzsvXWvwe6fd/HV3ltnJzCUhTAoGAZmJgWNiGD6gJVrNI79cEvVsL/gdd1YOacNQk1EVbJnj+Vu//6rJcVulkapOssm8kkF2sMBtZIPwj8hnlyN20hTWUgF0Wtg/5wN9bMMYOtU7MYTLKDomW02fVWkeRxM1LX3/X7Bv0uT4S6MWhpAgDARX0YQeRLpLvOCR5MOTy5r8";
        String elimentPubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCypUQM6UlRZRgffXNL0eo6SGSaaTa0kjDnBXGMWaLVShmrfXQXgJdguIFeoMd8gJpx3+j1fPkfbgnn1RG8lKJ3B7nyOqzSK139DaQutQ7a2EWpLD+iTZu/3R6Q/WEvW7iTOr1Nmr+JeS1YRegypadrOjvzwjva+UTB/StksP4uBwIDAQAB";
        /******************** 请求 start *********************/
        String url = "http://sit.elimen.com.cn:8899/openGateway";
//        String url = "http://localhost:9601/openGateway";
        String key = RandomStringTools.random(16);
        System.out.println("*******************key：" + key);
        String nonceStr = RandomStringTools.random(32);
        System.out.println("*******************nonceStr：" + nonceStr);
        // 创建壹链盟客户端
        ElimenTradeClient elimenTradeClient = new ElimenTradeClient("00000023", "elm.pmt.comm.file.upload", "UTF-8", "2021-08-17 17:60:00", "1.0",
                "", "", nonceStr, key, "RSA");

        // 拼接业务请求参数集
        ElimenTradeRequest request = new ElimenTradeRequest();
//        String bizCnt = "{\"fileName\":\"pikachu.txt\",\"fileType\":\"0006\",\"xfeType\":\"2\",\"fileCnt\":\"kIQB5SEAXf1R0f4juGeBaukgZrF8bYHzzxzUud6elo39HxnCOqhY78R/NJmhKipTP5sL1FXE+MYHqKE79JAXecVYYaM9IhAFXhuQsXM9n/msY0prE0yY7ZIMkKheWpjOEPDlQFrybMsP+DFycl4hPQyQrPESavStqW9XPyxDKRqkc9838jjL7KVSl6ME6NANrOpE7I58ZdA=\",\"fileCharset\":\"UTF-8\",\"fileData\":\"主流程\"}";
        String bizCnt = "{\"fileName\":\"pikachu.jpg\",\"fileType\":\"0001\",\"xfeType\":\"1\",\"fileCnt\":\"https://img-pre.ivsky.com/img/tupian/pre/201402/18/pikachu.jpg\",\"fileCharset\":\"UTF-8\",\"fileData\":{\"contName\":\"测试协议1\",\"sponsorName\":\"珠江投资\",\"acceptorName\":\"用友软件股份有限公司\",\"signOnline\":\"0\",\"signDate\":\"20200101\"}}";
        request.setBizCnt(bizCnt);

        // 获取加密请求报文
        String encryptedReqMsg = elimenTradeClient.getEncryptedReqMsg(request, elimentPubKey, channelKey);
        System.out.println("*********************encryptedReqMsg：" + encryptedReqMsg);
        String result = post(url, encryptedReqMsg);
        System.out.println(result);
        /******************** 请求 end *********************/

        /******************** 响应 start *********************/
        ElimenTradeClient elimenTradeClient2 = new ElimenTradeClient();
        Boolean verifyResult = elimenTradeClient2.verifySignStr(result, elimentPubKey);
        System.out.println("*********************verifyResult：" + verifyResult);
        // 获取解密响应报文
        ElimenTradeResponse elimenTradeResponse = elimenTradeClient2.getDecryptedRspMsg(result, channelKey);
        System.out.println("*********************elimenTradeResponse：" + JSONObject.toJSONString(elimenTradeResponse));
        /******************** 响应 end *********************/
    }

    private static String post(String url, String body) {
        String result = "";
        BufferedReader in = null;

        try {
            URL realUrl = new URL(url);
            URLConnection connection = realUrl.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(15000);
            connection.setRequestProperty("Content-type", "application/json;charset=UTF-8");
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.connect();
            OutputStreamWriter dataout = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
            if (body != null) {
                dataout.write(body);
            }

            dataout.flush();
            dataout.close();

            String line;
            for (in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8")); (line = in.readLine()) != null; result = result + line) {
            }

            if (in != null) {
                in.close();
            }

            return result;
        } catch (IOException var8) {
            return var8.getMessage();
        }
    }
}
